<?php

/*
 * Action - Attack 0$*
 *
 * You may trash 2 cards from your hand. If you do, +2 Cards, +2 Coin, and each other player discards down to 3 cards in hand. (This is not in the Supply)
 */

class Mercenary extends Card implements iAction, iDarkAges, iAttack {

    public function __construct() {
        parent::__construct("Mercenary", 0);
        $this->undoPlay = false;
        $this->undoBuy = false;
    }

    public function getDescription() {
        return array(
            "You may trash 2 cards from your hand. If you do, +2 Cards, +2 Coin, and each other player discards down to 3 cards in hand. (This is not in the Supply)");
    }

    public function play(Game $game, $isRealCard) {
        $game->attack($this);
        
        $playHand = $game->getPlayHand();
        if (sizeof($playHand) >= 2) {
            $options = array(new Option(1, "Yes"), new Option(2, "No"));
            $output = MySocket::GetUserReply($game, Input_ChooseOne, $options, 1, 1, null, "Trash 2 cards from you playhand?");
            $answer = $output[0];
            if ($answer == "Yes") {
                if (sizeof($playHand) == 2) {
                    foreach ($playHand as $c) {
                        $game->moveCardFromPileToPile($c, PlayHand, Trash);
                    }
                    ScreenDisplay::addScreenMessage($game, true, "trash " . nicePrint($playHand), "trashes".ScreenDisplay::$LMSG);
                } else {
                    $output = MySocket::GetUserReply($game, Input_ChooseMany, $playHand, 2, 2, null, "Choose cards to trash");
                    foreach ($output as $c) {
                        $game->moveCardFromPileToPile($c, PlayHand, Trash);
                    }
                    ScreenDisplay::addScreenMessage($game, true, "trash " . nicePrint($output), "trashes ".ScreenDisplay::$LMSG);
                }

                $game->addMoney(new Price(2));
                $drawed_cards = $game->drawCards(2);
                ScreenDisplay::addScreenMessage($game, false, ", get 2$ and draw " . nicePrint($drawed_cards), ", gets 2$ and draws " . sizeof($drawed_cards) . " cards");

                $game->setPhase(Phase_Sleep);

                $game->attack($this);

                $names = $game->getOtherPlayersNames(true);
                foreach ($names as $name) {
                    $playHandOther = $game->getPlayHand($name);
                    $playHandSize = sizeof($playHandOther);
                    if ($playHandSize > 3) {

                        $cards_to_discard = $playHandSize - 3;

                        $output = MySocket::GetUserReply($game, Input_ChooseMany, $playHandOther, $cards_to_discard, $cards_to_discard, null, "Choose $cards_to_discard cards to discard", $name, true);
                    }
                }
                
                $game->setPhase(Phase_Sleep);

                //the waiting function
                $users_replies = MySocket::getUsersReplies($game);
                foreach ($users_replies as $name => $output) {

                    foreach ($output as $card_name) {
                        $game->moveCardFromPileToPile($card_name, PlayHand, DiscardPile, $name);
                    }

                    ScreenDisplay::addScreenMessage($game, true, "discard " . nicePrint($output), "discards " . sizeof($output) . " cards", $name);

                    $game->setPhase(Phase_Sleep, $name);
                }
            } else {
                ScreenDisplay::addScreenMessage($game, true, "do nothing ", "does nothing");
            }
        } else {
            ScreenDisplay::addScreenMessage($game, true, "do nothing ", "does nothing");
        }

        $game->cardActionFinished();
        return true;
    }

}

?>